/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.Algorithms.Sorting
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using Generic.Utils;

	public interface ISwapper<T>
	{
		void Swap(T item1, T item2);
	}

	public class GenericListSwapper<T> : ISwapper<int>
	{
		public GenericListSwapper(IList<T> list)
		{
			_list = list;
		}

		#region ISwapper Members
		public void Swap(int item1, int item2)
		{
			if (item1 < 0 || item2 < 0)
				throw new IndexOutOfRangeException("Index of item1 or item2 must not be less then zero.");
			if (item1 == item2)
				return;
			T temp;
			temp = List[item1];
			List[item1] = List[item2];
			List[item2] = temp;
		}
		#endregion

		private IList<T> _list;
		public IList<T> List
		{
			get { return _list; }
			set { _list = value; }
		}
	}

    public class ListSwapper : ISwapper<int>
    {
        public ListSwapper(System.Collections.IList list)
        {
            ArgumentValidator.CheckIfNull<System.Collections.IList>(list);
            _list = list;
        }

        #region ISwapper<object> Members

        public void Swap(int item1, int item2)
        {
            if (item1 < 0 || item2 < 0)
                throw new IndexOutOfRangeException("Index of item1 or item2 must not be less then zero.");
            if (item1 == item2)
                return;
            object temp;
            temp = _list[item1];
            _list[item1] = _list[item2];
            _list[item2] = temp;
        }

        #endregion

        private System.Collections.IList _list;
        public System.Collections.IList List
        {
            get { return _list; }
            set { _list = value; }
        }

    }

}
